package com.zhouyu.mybatis.spring;

import com.zhouyu.mapper.OrderMapper;
import com.zhouyu.mapper.UserMapper;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;
import java.util.Map;

/**
 * @author leimingchao
 * create 2025-05-12 22:35
 *
 * Spring整合mybatis用到了ImportBeanDefinitionRegistrar
 *
 * ImportBeanDefinitionRegistrar必须结合@Import注解一起使用，单独配置为Bean不起作用
 * 使用@Import导入ImportBeanDefinitionRegistrar不会成为bean，不会有bean的生命周期
 */
public class ZhouyuImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

	/**
	 *
	 * @param importingClassMetadata 可以获取@Import注解所在类的其他注解信息
	 * @param registry current bean definition registry
	 * @param importBeanNameGenerator the bean name generator strategy for imported beans:
	 * {@link ConfigurationClassPostProcessor#IMPORT_BEAN_NAME_GENERATOR} by default, or a
	 * user-provided one if {@link ConfigurationClassPostProcessor#setBeanNameGenerator}
	 * has been set. In the latter case, the passed-in strategy will be the same used for
	 * component scanning in the containing application context (otherwise, the default
	 * component-scan naming strategy is {@link AnnotationBeanNameGenerator#INSTANCE}).
	 */
	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {

		//扫描路径
		Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(ZhouyuMapperScan.class.getName());
		String path = (String) annotationAttributes.get("value");


		ZhouyuBeanDefinitionScanner scanner = new ZhouyuBeanDefinitionScanner(registry);
		scanner.addIncludeFilter(new TypeFilter() {
			@Override
			public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
				return true;
			}
		});
		scanner.scan(path);

		//AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
		//beanDefinition.setBeanClass(ZhouyuFactoryBean.class);
		//beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(UserMapper.class);
		////userMapper ----> UserMapper的代理对象
		//registry.registerBeanDefinition("userMapper", beanDefinition);
		//
		//
		//AbstractBeanDefinition beanDefinition2 = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
		//beanDefinition2.setBeanClass(ZhouyuFactoryBean.class);
		//beanDefinition2.getConstructorArgumentValues().addGenericArgumentValue(OrderMapper.class);
		////orderMapper ----> OrderMapper的代理对象
		//registry.registerBeanDefinition("orderMapper", beanDefinition2);

	}

	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

	}
}
